<?php
class TopClient {
	
	/**
	 * 生成签名
	 *
	 * @param
	 *        	$paramArr：api参数数组
	 * @return $sign
	 */
	public function createSign($paramArr) {
		$sign = '';
		ksort ( $paramArr );
		foreach ( $paramArr as $key => $val ) {
			if ($key !== '' && $val !== '') {
				$sign .= $key . $val;
			}
		}
		$sign = strtoupper ( md5 ( TAO_APPSECRET . $sign . TAO_APPSECRET ) );
		return $sign;
	}
	
	/**
	 * 生成字符串参数
	 *
	 * @param
	 *        	$paramArr：api参数数组
	 * @return $strParam
	 */
	public function createStrParam($paramArr) {
		$strParam = '';
		foreach ( $paramArr as $key => $val ) {
			if ($key !== '' && $val !== '') {
				$strParam .= $key . '=' . urlencode ( $val ) . '&';
			}
		}
		
		return $strParam;
	}
	
	/**
	 * 以GET方式访问api服务
	 *
	 * @param
	 *        	$paramArr：api参数数组
	 * @return $result
	 */
	public function getResult($paramArr) {
		$sign = $this->createSign ( $paramArr );
		$strParam = $this->createStrParam ( $paramArr );
		$strParam .= 'sign=' . $sign;
		$result = $this->httpResult ( TAO_REST_URL . $strParam );
		return $this->getArrayData ( $result, $paramArr ['format'] );
	}
	
	public function getHttpsResult($paramArr) {
		$result = $this->httpsPostResult( TAO_REST_HTTPS_URL , $paramArr );
		//var_dump( $result );
		return $this->getArrayData ( $result, $paramArr ['format'] );
	}
	/**
	 * 以POST方式访问api服务
	 *
	 * @param
	 *        	$paramArr：api参数数组
	 * @return $result
	 */
	public function postResult($paramArr) {
		$sign = $this->createSign ( $paramArr );
		$paramArr ['sign'] = $sign;
		
		$result = '';
		
		return $result;
	}
	public function httpResult($url) {
		$curl = curl_init ( $url );
		curl_setopt ( $curl, CURLOPT_TIMEOUT, 60 );
		curl_setopt ( $curl, CURLOPT_USERAGENT, $_SERVER ['HTTP_USER_AGENT'] );
		curl_setopt ( $curl, CURLOPT_HEADER, false );
		curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, true );
		$content = curl_exec ( $curl );
		if(curl_exec($curl) === false)
		{
			echo 'Curl error: ' . curl_error($curl);
			exit;
		}
		
		curl_close ( $curl );
		return $content;
	}
	
	public function httpsPostResult( $url, $postFields = null)
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt ( $ch, CURLOPT_TIMEOUT, 60 );
		curl_setopt($ch, CURLOPT_FAILONERROR, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	
		if (is_array($postFields) && 0 < count($postFields))
		{
			$postBodyString = "";
			foreach ($postFields as $k => $v)
			{
				$postBodyString .= "$k=" . urlencode($v) . "&";
			}
			unset($k, $v);
			curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, false);
			curl_setopt($ch, CURLOPT_POST, true);
			curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
		}
		$reponse = curl_exec($ch);
		// 	if (curl_errno($ch)){
		// 		throw new Exception(curl_error($ch),0);
		// 	}
		// 	else{
		// 		$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		// 		if (200 !== $httpStatusCode){
		// 			throw new Exception($reponse,$httpStatusCode);
		// 		}
		// 	}
		curl_close($ch);
		return $reponse;
	}
	
	private function getArrayData($data, $format = 'json') {
		if ($format == 'xml') {
			$nodeName = substr ( $data, strpos ( $data, '?><' ) + 3, 100 );
			$nodeName = substr ( $nodeName, 0, strpos ( $nodeName, '>' ) );
			$xml = simplexml_load_string ( $data, 'SimpleXMLElement', LIBXML_NOCDATA );
			$tmp = $this->object2array ( $xml );
			$ret [$nodeName] = $tmp;
		} else {
			$ret = json_decode ( $data, true );
		}
		return $ret;
	}
	private function object2array($obj) {
		if (is_object ( $obj )) {
			foreach ( $obj->attributes () as $k => $v ) {
				if ($k == 'list' && count ( $obj->children () ) == 1) {
					$forceList = true;
					break;
				}
			}
			$obj = get_object_vars ( $obj );
		}
		
		if (is_array ( $obj )) {
			foreach ( $obj as $key => $value ) {
				if ($forceList && $key != '@attributes') {
					$obj [$key] = array (
							0 => $this->object2array ( $value ) 
					);
				} else {
					$obj [$key] = $this->object2array ( $value );
				}
			}
		}
		return $obj;
	}
}